<?php
/**
 * Object Relational Persistence Mapping Class for table: in_payment
 *
 * @package	core.ordo
 * @author	LHV
 */
class InPayment extends ORDataObject {

	/**#@+
	 * Fields of table: in_payment mapped to class members
	 */
	var $in_payment_id		= '';
	var $patient_id		    = '';
	var $original_price		= 0;
	var $discount		    = 0;
	var $insurance_price	= 0;
	var $patient_price		= 0;
	var $real_payment		= 0;
	var $enum_status		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'in_payment';

	/**
	 * Primary Key
	 */
	var $_key = 'in_payment_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'InPayment';

	/**
	 * Handle instantiation
	 */
	public function InPayment() {
		parent::ORDataObject();
		$objEm =   & Celini::enumManagerInstance();
        $statusOpen    =  $objEm->lookupKey(EnumerationConstant::IN_PAY_STATUS,
            EnumerationConstant::IN_PAY_STATUS_OPEN);
        $this->enum_status  =   $statusOpen;
	}
    /**#@+
     * Getters
     */
	public function getPatientId() {
		return $this->patient_id;
	}

    public function getOriginalPrice() {
        return $this->original_price;
    }

    public function getDiscount() {
        return $this->discount;
    }

    public function getInsurancePrice() {
        return $this->insurance_price;
    }

    public function getPatientPrice() {
        return $this->patient_price;
    }

    public function getRealPayment() {
        return $this->real_payment;
    }
	/**#@-*/

    /**#@+
     * Setters
     */
    public function setPatientId($val) {
        $this->patient_id   =   $val;
    }

    public function setOriginalPrice($val) {
        $this->original_price   =   $val;
    }

    public function setDiscount($val) {
        $this->discount   =   $val;
    }

    public function setInsurancePrice($val) {
        $this->insurance_price   =   $val;
    }

    public function setPatientPrice($val) {
        $this->patient_price   =   $val;
    }

    public function setRealPayment($val) {
        $this->real_payment   =   $val;
    }
    /**#@-*/

    public function getPaymentByPatient($patientId) {
        $objEm =   & Celini::enumManagerInstance();
        $statusOpen    =  $objEm->lookupKey(EnumerationConstant::IN_PAY_STATUS,
            EnumerationConstant::IN_PAY_STATUS_OPEN);
        $sql    =   "SELECT $this->_key FROM $this->_table
                    WHERE patient_id=$patientId AND enum_status='$statusOpen'";
        $res    =   $this->_execute($sql);
        $obj    =   Celini::newORDO('InPayment', $res->fields["$this->_key"]);

        return $obj;
    }

    public function updateRealPayment(){
    	$sql   =   "UPDATE $this->_table
    	           SET real_payment=(SELECT sum(amount) FROM in_payment_collection WHERE $this->_key=$this->in_payment_id)
    	           WHERE $this->_key=$this->in_payment_id";
        $this->_execute($sql);
    }
}
?>
